//快速创建web服务器的第三方包
var express = require('express')

var app = express()
//设置跨域访问中间件,修改响应头
app.use((req, res, next)=>{

    res.header("Access-Control-Allow-Origin", "*");
    
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    
    res.header("X-Powered-By",' 3.2.1');
    
    res.header("Content-Type", "application/json;charset=utf-8");
    
    next();
    
    });

app.get('/123', function (req, res) {
    res.status(200),
    res.json({ username: '陈思元' })
});

//jsonp的写法
app.get('/getUser',async(requst,res)=>{
    //从请求头里拿返的函数
    let callback = requst.query.callback
    let fn = callback+`({username:'袁六六'})`
    res.send(fn)
})

//app监听端口号8888
app.listen(8888, () => {
    console.log('http://localhost:8888');
})

var app1 = express()
//监听7777端口,将当前文件夹作为http服务,不指定默认是index.html
app1.use(express.static(__dirname))
app1.listen(7777)


